package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.entity.SysDeployForm;
import com.ruoyi.entity.SysForm;
import com.ruoyi.service.FlowDefinitionServiceImpl;
import com.ruoyi.service.SysDeployFormServiceImpl;
import com.ruoyi.service.SysFormServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * 流程表单Controller
 *
 * @author XuanXuan
 *
 */
@Slf4j
@RestController
@RequestMapping("/flowable/form")
public class SysFormController extends BaseController {
    @Autowired
    private SysFormServiceImpl sysFormService;

    @Autowired
    private SysDeployFormServiceImpl sysDeployFormService;

    @Autowired
    private FlowDefinitionServiceImpl flowDefinitionService;


    /**
     * 查询流程表单列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SysForm sysForm) {
        startPage();
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        return getDataTable(list);
    }

    /**
     * 导出流程表单列表
     */


    @GetMapping("/export")
    public AjaxResult export(SysForm sysForm) {
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        ExcelUtil<SysForm> util = new ExcelUtil<SysForm>(SysForm.class);
        return util.exportExcel(list, "form");
    }

    /**
     * 获取流程表单详细信息
     */

    @GetMapping(value = "/{formId}")
    public AjaxResult getInfo(@PathVariable("formId") Long formId) {
        return AjaxResult.success(sysFormService.selectSysFormById(formId));
    }

    /**
     * 获取流程表单详细信息
     */
    // 
    @GetMapping(value = "/getFormFlowInfo")
    public AjaxResult getFormFlowInfo(Long formId) throws IOException {
        return AjaxResult.success(sysFormService.getFormFlowInfo(formId));
    }

    /**
     * 新增流程表单
     */


    @PostMapping
    public AjaxResult add(@RequestBody SysForm sysForm) {
        sysFormService.insertSysForm(sysForm);
        return toAjax(1);
    }

    /**
     * 修改流程表单
     */


    @PutMapping
    public AjaxResult edit(@RequestBody SysForm sysForm) {
        return toAjax(sysFormService.updateSysForm(sysForm));
    }

    /**
     * 删除流程表单
     */


    @DeleteMapping("/{formIds}")
    public AjaxResult remove(@PathVariable Long[] formIds) {
        return toAjax(sysFormService.deleteSysFormByIds(formIds));
    }


    /**
     * 挂载流程表单
     */

    @PostMapping("/addDeployForm")
    public AjaxResult addDeployForm(@RequestBody SysDeployForm sysDeployForm) {
        return toAjax(sysDeployFormService.insertSysDeployForm(sysDeployForm));
    }

    @PutMapping(value = "/updateState")
    public AjaxResult updateState(@RequestParam String state,
                                  @RequestParam Long formId) {
        SysForm sysForm = new SysForm();
        sysForm.setState(state);
        sysForm.setFormId(formId);
        sysFormService.updateById(sysForm);
        return AjaxResult.success();
    }


}
